<template>
  <div class="mod-config">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="queryList()">
      <div style="    position: absolute;   right: 20px;">
        <el-form-item>
          <el-select v-model="searchForm.bizCode" placeholder="请选择业务系统" style="width: 100%" @change="queryList()">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="(item, index) in dict.BizGroups" :key="index"
                       :label="item.value+':'+item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自动刷新" style="width:160px">
          <el-select  v-model="searchForm.autoLoad" style="width:80px"
                      @change="startLoop()">
            <el-option label="关闭" value="0"></el-option>
            <el-option label="5秒" value="5"></el-option>
            <el-option label="10秒" value="10"></el-option>
          </el-select>
        </el-form-item>
      </div>

      <el-form-item>
        <el-input v-model="searchForm.batchCode" placeholder="批次编号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.batchType" filterable placeholder="请选择批次类型" style="width:100%">
          <el-option label="全部" value=""></el-option>
          <template v-for="(item, index) in dict.BatchType">
            <el-option :label="item.value +': '+ item.label" :value="item.value"></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.execStatus" filterable placeholder="请选择运行状态" style="width:100%">
          <el-option label="全部" value=""></el-option>
          <template v-for="(item, index) in dict.ExecStatus">
            <el-option :label="item.value +': '+ item.label" :value="item.value"></el-option>
          </template>
        </el-select>
      </el-form-item>
      <!--<el-form-item>
        <el-input v-model="searchForm.execDate" placeholder="执行日期" clearable></el-input>
      </el-form-item>-->
      <el-form-item>
        <el-date-picker
          v-model="searchForm.execDate"
          type="date"
          value-format="yyyyMMdd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryList(1)">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="singleTable"
      :data="dataList"
      border
      @row-click="handleCurrentChange"
      v-loading="dataListLoading"
      style="width: 100%;">
      <!--           <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
      <!--           <el-table-column prop="id"  label="主键" header-align="center" align="center"></el-table-column>-->
      <!--           <el-table-column prop="bizCode"  label="业务系统编号" header-align="center" align="center"></el-table-column>-->
      <el-table-column prop="batchCode" label="批次编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="batchType" label="类型" header-align="center" align="center">
        <template slot-scope="scope">
          <div style="cursor: pointer;">
            <template v-for="(item, index) in dict.BatchType">
              <el-tag v-if="item.value == scope.row.batchType" :type="item.style">
                {{item.value}}: {{item.label}}
              </el-tag>
            </template>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="scheduleType" label="执行方式" header-align="center" align="center">
        <template slot-scope="scope">
          <div style="cursor: pointer;">
            <template v-for="(item, index) in dict.ScheduleType">
              <el-tag v-if="item.value == scope.row.scheduleType" :type="item.style">
                {{item.value}}: {{item.label}}
              </el-tag>
            </template>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="execDate" label="执行日期" header-align="center" align="center"></el-table-column>
      <el-table-column prop="batchTriggerTime" label="批次号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="startupTime" label="启动时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="execStatus" label="执行状态" header-align="center" align="center">
        <template slot-scope="scope">
          <template v-for="(item, index) in dict.ExecStatus">
            <el-tag v-if="item.value == scope.row.execStatus" :type="item.style">
              {{item.value}}: {{item.label}}
            </el-tag>
          </template>
        </template>
      </el-table-column>
      <!--           <el-table-column prop="execResult"  label="执行结果" header-align="center" align="center"></el-table-column>-->

      <!--       <el-table-column prop="endTime" label="结束时间" header-align="center" align="center"></el-table-column>-->
      <el-table-column prop="execTime"  label="执行耗时" header-align="center" align="center" width="110">
      <template slot-scope="scope">
        <el-tooltip placement="bottom-end">
          <div slot="content">开始时间：{{scope.row.startupTime}}<br/>结束时间：{{scope.row.endTime}}</div>
          <div>{{scope.row.execTime}}</div>
        </el-tooltip>
      </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="160" >
        <template slot-scope="scope">
          <!--             	<el-button type="text" size="small" @click="modEntity(scope.row)">修改</el-button>-->
          <el-button type="text" size="small" @click="showDetail(scope.row)">明细 &nbsp;&nbsp;</el-button>
          <!--<el-button type="text" size="small" @click="showDetail(scope.row)">n暂停</el-button>
          <el-button type="text" size="small" @click="showDetail(scope.row)">n续跑</el-button>-->
          <!--<el-button type="text" size="small" @click="showDetail(scope.row)">n终止</el-button>-->
          <!--<el-button type="text" size="small" @click="showDetail(scope.row)">n重跑</el-button>-->
          <span @mouseenter="queryNext(scope.row)" @click.stop style="padding-left: 6px;">
            <el-tooltip class="item" effect="dark" placement="bottom-end">
              <div slot="content" v-html="rowNextText"></div>
              <el-button type="text" size="small" @click.stop>下次执行时间</el-button>
            </el-tooltip>
          </span>
          <!--                <el-button type="text" size="small" @click="deleteEntity(scope.row.id)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-entity v-if="addEntityVisible" ref="addEntity" @refreshDataList="queryList"></add-entity>
    <mod-entity v-if="modEntityVisible" ref="modEntity" @refreshDataList="queryList"></mod-entity>
    <show-detail v-if="showDetailVisible" ref="showDetail"></show-detail>
  </div>
</template>

<script>
  //import AddEntity from './batch-log-add'
  //import ModEntity from './batch-log-mod'
  //import ShowDetail from './batch-log-detail'
  import {mapState} from "vuex";

  export default {
    data() {
      return {
        timer: null,
        showTip: false,
        isShow: false,
        searchForm: {
          autoLoad: '10',
        },
        dataList: [],
        rowNextText: '....加载数据....',
        page: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addEntityVisible: false,
        modEntityVisible: false,
        showDetailVisible: false
      }
    },
    components: {
      //AddEntity, ModEntity, ShowDetail
    },
    created() {
      if (this.timer) {
        clearTimeout(this.timer)
      }
    },
    beforeDestroy() {
      if (this.timer) {
        clearTimeout(this.timer)
      }
    },
    activated() {
      this.queryList()
    },
    computed: mapState({
      // listType: state => state.goods.listType
    }),
    mounted() {
      // 数据字典查询
      //this.getSelectDict('FlowType').then((res) => {
      //  this.$store.commit('goods/updateListType', res)
      //})
      if (this.timer) {
        clearTimeout(this.timer)
      }
    },
    methods: {
      startLoop() {
        let time = parseInt(this.searchForm.autoLoad);
        clearTimeout(this.timer)
        this.timer = null;
        console.info(time)
        if (time < 3) {
          return;
        } else {
          this.timer = setTimeout(this.queryList, time * 1000);
        }
      },
      handleCurrentChange(val) {
        let rowData = Object.assign({}, val)
        window.sessionStorage.setItem("__Log_Batch_exec", JSON.stringify(rowData))
        this.$router.push({ name: 'task-exec-log' , params: rowData});
      },
      // 获取数据列表
      queryList(clr) {
        if (typeof (clr) != 'undefined') {
          this.page = 1
        }
        this.dataListLoading = true
        let obj = {
          'page': this.page,
          'size': this.pageSize,
          //isTrans: true,
          //transParams: ['FlowType', 'ApplyResult', 'ReceiveStatus'],
          //transDict: ['flowType', 'applyResult', 'receiveStatus']
        }
        obj = Object.assign(obj, this.searchForm)
        this.getTableList('/api/batchExecLog/list', obj, (data) => {
          this.dataListLoading = false
          if (data.code == '000000' && data.rows && data.rows.length > 0) {
            this.dataList = data.rows
            this.totalPage = data.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.startLoop();
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.page = 1
        this.queryList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.page = val
        this.queryList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      showDetail(row) {
        let cloneData = Object.assign({}, row)
        this.showDetailVisible = true
        this.$nextTick(() => {
          this.$refs.showDetail.init(cloneData)
        })
      },
      queryNext(row){
        this.rowNextText = '...加载中...';
        let obj = {
          'bizCode': row.bizCode,
          'batchCode': row.batchCode
        }
       this.getTableList('/api/batchInfo/nextTime', obj, (data) => {
          if (data.code == '000000' && data.nextTimes && data.nextTimes.length > 0) {
            let rs = "";
            data.nextTimes.forEach(it=>{
              rs = rs + it + "<br/>";
            });
            this.rowNextText = rs;
          }
        })
      },

    }
  }
</script>